//
//  DoorEnginerViewController.m
//  RONGAO
//
//  Created by wei wang on 2017/2/23.
//  Copyright © 2017年 wei wang. All rights reserved.
//

#import "DoorEnginerViewController.h"
#import "DoorCollectionViewCell.h"
#import "GatherViewController.h"
#import "TanChuangView.h"
#import "RongGaoGit.h"
@interface DoorEnginerViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
/** UIcollectionView **/
@property(nonatomic,strong) UICollectionView *collectionView;

/** describeArray **/
@property(nonatomic,strong) NSArray *describeArray;

/** index **/
@property(nonatomic,assign) NSInteger obcIndex;
@end

static NSString *Cell = @"Cell";

@implementation DoorEnginerViewController
- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    [self.collectionView reloadData];
    
}


- (void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:animated];

    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setNavigationStyle];
    [self masLayoutSubview];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateTableView) name:@"updateTableView" object:nil];
}

- (void)updateTableView{
    
    [self.collectionView reloadData];
}

- (void)setNavigationStyle{
    
    [self setNavigationTitle:@"工程设置" titleColor:nil];
    [self setLeftButtonImage:[UIImage imageNamed:@"icon_back_nor"]];

}

#pragma mark - --------------------------UICollectionViewDataSource--------------------------

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section{
    return self.describeArray.count;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    
    return CGFLOAT_MIN;
    
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    
    return CGFLOAT_MIN;
    
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    /*
     
     if count > 3
     return CGSizeMake(collectionView.frame.size.width/3, collectionView.frame.size.height);
     
     eles
     
     return CGSizeMake(collectionView.frame.size.width/count, collectionView.frame.size.height);
     */
    
    return CGSizeMake(collectionView.frame.size.width/3, 100);
    
    
}

#pragma mark - --------------------------UICollectionViewDelegate--------------------------

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    DoorCollectionViewCell *cell = [cv dequeueReusableCellWithReuseIdentifier:Cell forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    [cell setupView];
    DoorStatusModel *stautsModel = [RongGaoGit getDoorStatusModel];

    
    cell.describeLabel.text = self.describeArray[indexPath.row];
    switch (indexPath.row) {
        case 0:
        case 1:
        case 2:
        {
            cell.TitleLabel.text = [NSString hexadecimalToDecimal:[stautsModel sendGetMethodWithPropertyName:[stautsModel propertyOfSelf][indexPath.row]] hexadecimalLength:4];
            break;
        }
        case 3:
        {
            NSString *indexInfo = [NSString hexadecimalToDecimal:[stautsModel sendGetMethodWithPropertyName:[stautsModel propertyOfSelf][indexPath.row]] hexadecimalLength:4];
            
            if ([indexInfo intValue] == 0) {
                
                cell.TitleLabel.text = @"反开门";
            }else if ([indexInfo intValue] == 1){
                cell.TitleLabel.text = @"正开门";
                
            }
        }
            break;
            
        case 4:
        {
            
            cell.TitleLabel.text = [NSString hexadecimalToDecimal:[stautsModel sendGetMethodWithPropertyName:[stautsModel propertyOfSelf][indexPath.row]] hexadecimalLength:4];

        }
            break;
            
        case 5:
        case 6:
        case 7:
        {
            NSString *indexInfo = [NSString hexadecimalToDecimal:[stautsModel sendGetMethodWithPropertyName:[stautsModel propertyOfSelf][indexPath.row]] hexadecimalLength:4];
            
            if ([indexInfo intValue] == 0) {
                
                cell.TitleLabel.text = @"不开启";
            }else if ([indexInfo intValue] == 1){
                cell.TitleLabel.text = @"开启";
                
            }

        }
            break;
        case 8:
        {
            NSString *indexInfo = [NSString hexadecimalToDecimal:[stautsModel sendGetMethodWithPropertyName:[stautsModel propertyOfSelf][indexPath.row]] hexadecimalLength:4];
            if ([indexInfo intValue] == 0) {
                
                cell.TitleLabel.text = @"重型";
            }else if ([indexInfo intValue] == 1){
                cell.TitleLabel.text = @"轻型";
                
            }
            
        }
            break;
        case 9:{
            
            NSString *indexInfo = [NSString hexadecimalToDecimal:[stautsModel sendGetMethodWithPropertyName:[stautsModel propertyOfSelf][indexPath.row]] hexadecimalLength:4];
            if ([indexInfo intValue] == 0) {
                
                cell.TitleLabel.text = @"手动";
            }else if ([indexInfo intValue] == 1){
                cell.TitleLabel.text = @"自动";
                
            }
            
        }
            break;
            
        case 10:
        {
            cell.TitleLabel.text = @"复位";
            
        }
            break;
        default:
            
            break;
    }
    
    
    return cell;
}




- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == self.describeArray.count-1) {
        
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"是否复位" message:@"" preferredStyle:UIAlertControllerStyleAlert];
        

        __weak typeof(self) weakself = self;
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
          
            [[AppDelegate globalDelegate] sendCommandByFunctionCode:@"100B" functionCommand:@"0601"];

            
            [weakself.collectionView reloadData];
        }];
        
        [alertController addAction:cancelAction];
        [alertController addAction:okAction];
        [self presentViewController:alertController animated:YES completion:nil];

        
        
      
    }else{
        
        
        
        GatherViewController *VC = [[GatherViewController alloc] init];
        VC.hidesBottomBarWhenPushed = YES;
        VC.selectIndex = indexPath.row;
        [VC setGatherVCTitleAndPickerType:indexPath.row];
        [self.navigationController pushViewController:VC animated:YES];
        
    }
    
}

- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

// cell点击变色
- (void)collectionView:(UICollectionView *)colView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell* cell = [colView cellForItemAtIndexPath:indexPath];
    //设置(Highlight)高亮下的颜色
    NSArray *ary=[cell subviews];
    for (id obc in ary) {
        if ([obc isKindOfClass:[UILabel class]]) {
            UILabel *tem=((UILabel *)obc);
            tem.textColor = [UIColor whiteColor];
        }
    }
    
    [cell setBackgroundColor:[UIColor colorWithHexString:@"#dab866"]];
}

- (void)collectionView:(UICollectionView *)colView  didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell* cell = [colView cellForItemAtIndexPath:indexPath];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [cell setBackgroundColor:[UIColor whiteColor]];
        
        NSArray *ary=[cell subviews];
        for (id obc in ary) {
            if ([obc isKindOfClass:[UILabel class]]) {
                UILabel *tem=((UILabel *)obc);
                tem.textColor = [UIColor colorWithHexString:@"#56504f"];
            }
        }
        
    });
    
}

- (UICollectionView *)collectionView{
    
    if (!_collectionView) {
        
        UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        
        _collectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:flowLayout];
        [_collectionView registerClass:[DoorCollectionViewCell class] forCellWithReuseIdentifier:Cell];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.alwaysBounceVertical = YES;
        [self.view addSubview:self.collectionView];
        
    }
    
    return _collectionView;
}

- (NSArray *)describeArray{
    
    if (!_describeArray) {
        
        _describeArray = @[@"学习阶段出力系数",@"运行阶段出力系数",@"最大电流限制",@"开门方向选择",@"遇阻灵敏度",@"开门遇阻",@"关门遇阻",@"电锁开启",@"轻重型门选择",@"PID学调节模式",@"复位命令"];
        
    }
    
    return _describeArray;
}

- (void)masLayoutSubview{
    
    __weak UIView *superView = self.view;
    
    [self.collectionView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(superView.mas_top);
        make.left.mas_equalTo(superView.mas_left);
        make.right.mas_equalTo(superView.mas_right);
        make.bottom.mas_equalTo(superView.mas_bottom);
        
    }];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
